package gac.helloworld;

import android.app.Activity;
import android.content.Intent;
import android.database.Cursor;
import android.os.Bundle;
import android.util.Log;
import android.view.View;
import android.widget.EditText;
import android.widget.TimePicker;

public class MainActivity extends Activity {

	private EditText mAddress;
	private EditText mWarningTime;
	private TimePicker mTimePick;
	private double currentLatitude;
	private double currentLongitude;
	private MapHandlerActivity map;
	private String destination;
	private int finalTime;
	private int bufferTime;
	private AlarmDbAdapter mDbHelper = null;
	private Long mRowId = null;

	/** Called when the activity is first created. */
	@Override
	public void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		setContentView(R.layout.main);

		Intent intent = getIntent();
		if (intent != null)
		{
			Bundle bundle = intent.getExtras(); 
			if (bundle != null)
				mRowId = bundle.getLong(AlarmDbAdapter.KEY_ROWID);
		}

		mDbHelper = new AlarmDbAdapter(this);
		mDbHelper.open();		

		mAddress = (EditText) findViewById(R.id.address_text);
		mWarningTime = (EditText) findViewById(R.id.warn_time);
		mTimePick = (TimePicker) findViewById(R.id.arrival_time);
		mTimePick.setIs24HourView(true);
		LocateService.setMainActivity(this);
		populateFields();
	}

	private void populateFields() {
		if (mRowId != null) {
			Cursor c = mDbHelper.fetchAlarm(mRowId);
			startManagingCursor(c);
			
			mAddress.setText(c.getString(
					c.getColumnIndexOrThrow(AlarmDbAdapter.KEY_DESTINATION)));

			int warningTime = c.getInt(c.getColumnIndexOrThrow(AlarmDbAdapter.KEY_WARNTIME));
			if (warningTime != -1) // if there's a warning time
				mWarningTime.setText("" + warningTime);
			
			mTimePick.setCurrentHour(c.getInt(
					c.getColumnIndexOrThrow(AlarmDbAdapter.KEY_HOUR)));

			mTimePick.setCurrentMinute(c.getInt(
					c.getColumnIndexOrThrow(AlarmDbAdapter.KEY_MINUTE)));
			
		}

		map = new MapHandlerActivity(this);
	}

	public void saveClick(View v) {		
		// make sure the fields aren't null
		String warnTimeStr = mWarningTime.getText().toString();

		//if (warnTimeStr == "" || warnTimeStr.length() == 0);
		//	warnTimeStr = "" + AlarmData.NO_WARN_TIME;

		int warnTime = AlarmData.NO_WARN_TIME;
		try {
			warnTime = Integer.parseInt(warnTimeStr);
		} catch (NumberFormatException e) { }

		int minute = mTimePick.getCurrentMinute();
		int hour = mTimePick.getCurrentHour();
		Log.v("MainActivity saveClick", "saving time  " + hour + ":" + minute);
		String destination = mAddress.getText().toString();

		if (mRowId == null)
			mDbHelper.createAlarm(destination, hour, minute, warnTime,
					AlarmData.PM);
		else
			mDbHelper.updateAlarm(mRowId, destination, hour, minute, warnTime,
					AlarmData.PM);

		Intent i = new Intent(this, LocateService.class);
		Bundle bundle = new Bundle();
		bundle.putInt(AlarmDbAdapter.KEY_WARNTIME, warnTime);
		bundle.putInt(AlarmDbAdapter.KEY_HOUR, hour);
		bundle.putInt(AlarmDbAdapter.KEY_MINUTE, minute);
		bundle.putString(AlarmDbAdapter.KEY_DESTINATION, destination);
		i.putExtras(bundle);
		startService(i);
		setResult(RESULT_OK);
		finish();
	}

	public void clearClick(View v){
		mAddress.setText("");
		mWarningTime.setText("");
		mTimePick.setCurrentHour(0);
		mTimePick.setCurrentMinute(0);
	}
}